<?php

namespace UFSW\TorneoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use UFSW\DataBaseBundle\Entity\Torneo;
use UFSW\TorneoBundle\Form\TorneoType;

use Symfony\Component\HttpFoundation\Request;

class TorneoController extends Controller
{
    /**
     * Vistas: son los archivos html a los que vamos a redireccionar repetidas veces
     */
    protected $indexVista;
    protected $verVista;
    protected $nuevoVista;
    protected $editarVista;
    
    public function __construct() {
        $this->indexVista = 'TorneoBundle:Torneo:index.html.twig';
        $this->portadaVista = null;
        $this->verVista = 'TorneoBundle:Torneo:show.html.twig';
        $this->nuevoVista = 'TorneoBundle:Torneo:new.html.twig';
        $this->editarVista = 'TorneoBundle:Torneo:edit.html.twig';
    }
    
    /**
     * Lists all Torneo entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $torneos = $em->getRepository('UFSWDataBaseBundle:Torneo')->findAll();

        return $this->render($this->indexVista, array(
            'torneos' => $torneos,
        ));
    }
    /**
     * Creates a new Torneo entity.
     *
     */
    public function createAction(Request $request)
    {
        $torneo = new Torneo();
        $form = $this->createCreateForm($torneo);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $torneo->setEstado(true);
            $torneo->setUsuario($this->getUser());
            $em = $this->getDoctrine()->getManager();
            $em->persist($torneo);
            $em->flush();

            return $this->redirect($this->generateUrl('torneo_show', array('id' => $torneo->getId())));
        }

        return $this->render($this->nuevoVista, array(
            'torneo' => $torneo,
            'form'   => $form->createView(),
        ));
    }

    /**
    * Creates a form to create a Torneo entity.
    *
    * @param Torneo $torneo The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Torneo $torneo)
    {
        $form = $this->createForm(new TorneoType(), $torneo, array(
            'action' => $this->generateUrl('torneo_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => $this->get('translator')->trans('create')));

        return $form;
    }

    /**
     * Displays a form to create a new Torneo entity.
     *
     */
    public function newAction()
    {
        $torneo = new Torneo();
        $form   = $this->createCreateForm($torneo);

        return $this->render($this->nuevoVista, array(
            'torneo' => $torneo,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Torneo entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $torneo = $em->getRepository('UFSWDataBaseBundle:Torneo')->find($id);

        if (!$torneo) {
            throw $this->createNotFoundException('Unable to find Torneo entity.');
        }

        return $this->render('TorneoBundle:Torneo:show.html.twig', array(
            'torneo'      => $torneo,
            ));
    }

    /**
     * Displays a form to edit an existing Torneo entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $torneo = $em->getRepository('UFSWDataBaseBundle:Torneo')->find($id);

        if (!$torneo) {
            throw $this->createNotFoundException('Unable to find Torneo entity.');
        }

        $editForm = $this->createEditForm($torneo);

        return $this->render('TorneoBundle:Torneo:edit.html.twig', array(
            'torneo'      => $torneo,
            'edit_form'   => $editForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a Torneo entity.
    *
    * @param Torneo $torneo The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Torneo $torneo)
    {
        $form = $this->createForm(new TorneoType(), $torneo, array(
            'action' => $this->generateUrl('torneo_update', array('id' => $torneo->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => $this->get('translator')->trans('update')));

        return $form;
    }
    /**
     * Edits an existing Torneo entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $torneo = $em->getRepository('UFSWDataBaseBundle:Torneo')->find($id);

        if (!$torneo) {
            throw $this->createNotFoundException('Unable to find Torneo entity.');
        }

        $editForm = $this->createEditForm($torneo);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('torneo_edit', array('id' => $id)));
        }

        return $this->render('TorneoBundle:Torneo:edit.html.twig', array(
            'torneo'      => $torneo,
            'edit_form'   => $editForm->createView(),
        ));
    }
   
}
